//
//  UserListViewController.m
//  My - douban
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 张鸿峰. All rights reserved.
//

#import "UserListViewController.h"


#import "LoginViewController.h"

#import "DataBaseSingleTon.h"

#import "ActivityDetailViewController.h"

#import "MyActivityViewController.h"


@interface UserListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain) UITableView *myTabelView;


@property (nonatomic,assign) BOOL selected;

@property (nonatomic, retain) NSMutableArray *arrM;


@end

@implementation UserListViewController

- (void)dealloc
{
    [_myTabelView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"我的";
    
    [self.myTabelView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction:)];
    
    self.selected = NO;
    
    
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName1"];
    //NSString *sql = @"SELECT * FROM activityStore";
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM activityStore WHERE userName = '%@'", isLogin];
    
    
    self.arrM = [[DataBaseSingleTon shareDataBase] selectActivityStoreSQL:sql];
    
}

- (void)rightBarButtonAction:(UIBarButtonItem *)bar
{
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [self.navigationController pushViewController:loginVC animated:YES];
    
    [loginVC release];
    
}

- (UITableView *)myTabelView
{
    if (_myTabelView == nil) {
        
        self.myTabelView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
        self.myTabelView.delegate = self;
        self.myTabelView.dataSource = self;
        [self.view addSubview:self.myTabelView];
        [self.myTabelView release];
    }
    return _myTabelView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [self.myTabelView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的活动";
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = @"清除缓存";
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        MyActivityViewController *myActivityVC = [[MyActivityViewController alloc] init];
        [self.navigationController pushViewController:myActivityVC animated:YES];
        [myActivityVC release];
    }
    
    
    if (indexPath.row == 1) {
        
        UIAlertController *alerC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认注销" preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alerC animated:YES completion:^{
            
            
            
            
        }];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            
            NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
            
            NSString *userName = [userDefault objectForKey:@"userName1"];
            
            [userDefault setObject:@"" forKey:@"userName1"];
            
            [[DataBaseSingleTon shareDataBase] dropTableUser:userName]; //删除当前用户
            
            [[DataBaseSingleTon shareDataBase] dropActivityStoreTable:userName]; //删除收藏的活动
            
            
        }];
        [alerC addAction:action];
        [alerC addAction:action1];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
